﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Xml;
using System.Text.RegularExpressions;

namespace Cwing.UI.Validation.Manager
{
    internal class ControlValidationManager
    {
        private IValidation validation;

        private String value = String.Empty;

        public String Value
        {
            get
            {
                return value;
            }
        }

        public Configuration ValidationConfiguration { get; set; }

        public ControlValidationManager(String value, IValidation validation)
            : this(value, validation, ValidationConfigHelper.FetchConfigNode(validation.Path))
        {

        }

        public ControlValidationManager(String value, IValidation validation, Configuration configuration)
        {
            this.value = value;
            this.validation = validation;
            ValidationConfiguration = configuration;
        }

        public ValidationResult Validate()
        {
            if (ValidationConfiguration == null)
            {
                return ValidationResult.Empty;
            }
            else
            {
                ValidationResult result = new ValidationResult();

                //Validate Mandatory
                if (String.IsNullOrEmpty(Value))
                {
                    if (!ValidationConfiguration.Mandatory)
                    {
                        changeBgColor(String.Empty);
                        //validation.Self.Style["background-Color"] = "";

                        result.Result = true;
                        result.Message = String.Empty;
                        return result;
                    }
                    else
                    {
                        changeBgColor(ValidationConfigHelper.section.MandatoryColor);
                        //validation.Self.Style["background-Color"] = ValidationConfigHelper.section.MandatoryColor;
                        result.Result = false;
                        result.Message = ValidationConfiguration.MandatoryMessage;
                        return result;
                    }
                }

                //Validate Length
                if (ValidationConfiguration.MaxLength == ValidationConfiguration.MinLength)
                {
                    if (Value.Length != ValidationConfiguration.MaxLength)
                    {
                        changeBgColor(ValidationConfigHelper.section.FailureColor);
                        //validation.Self.Style["background-Color"] = ValidationConfigHelper.section.FailureColor;
                        result.Result = false;
                        result.Message = ValidationConfiguration.LengthMessage;
                        return result;
                    }
                }

                //If Length both equal 0, pass
                if (ValidationConfiguration.MaxLength > ValidationConfiguration.MinLength)
                {
                    if (value.Length < ValidationConfiguration.MinLength || value.Length > ValidationConfiguration.MaxLength)
                    {
                        changeBgColor(ValidationConfigHelper.section.FailureColor);
                        //validation.Self.Style["background-Color"] = ValidationConfigHelper.section.FailureColor;
                        result.Result = false;
                        result.Message = ValidationConfiguration.LengthMessage;
                        return result;
                    }
                }

                if (!String.IsNullOrEmpty(ValidationConfiguration.TypeRegEx))
                {
                    Regex regex = new Regex(ValidationConfiguration.TypeRegEx);
                    result.Result = regex.IsMatch(value);
                    if (!result.Result)
                    {
                        changeBgColor(ValidationConfigHelper.section.FailureColor);
                        //validation.Self.Style["background-Color"] = ValidationConfigHelper.section.FailureColor;
                        result.Message = ValidationConfiguration.TypeMessage;
                        return result;
                    }
                }
                changeBgColor(String.Empty);
                //validation.Self.Style["background-Color"] = "";

                result.Result = true;
                result.Message = String.Empty;
                return result;
            }
        }

        private void changeBgColor(String color)
        {
            if (validation != null && validation.Self != null)
            {
                validation.Self.Style["background-Color"] = color;
            }
        }
    }
}
